package com.maoye.ossp.common.web.taglib;

import java.io.IOException;
import java.util.List;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hotent.base.core.util.string.StringUtil;
import com.hotent.bpmx.api.model.identity.BpmIdentity;
import com.hotent.bpmx.api.model.process.task.BpmTask;
import com.hotent.bpmx.api.service.BpmTaskService;
import com.maoye.org.core.service.MoiUserService;

/**
 * 通过流程实例ID查询处理人信息
 * 
 * @author yangyy2
 * 
 */
public class HandleMsgTag extends RequestContextAwareTag {

	private static final long serialVersionUID = 1L;
	/**
	 * 流程实例ID
	 */
	private String proInstID;

	private MoiUserService moiUserService;
	
	private BpmTaskService bpmTaskService;

	@Override
	protected int doStartTagInternal() throws Exception {
		try {
			StringBuilder handler = new StringBuilder();
			StringBuilder handleName = new StringBuilder();
			List<BpmTask> taskList = getBpmTaskService().getTasksInstId(proInstID);
			for(BpmTask task : taskList) {
				if(StringUtil.isNotEmpty(handleName.toString()))
					handleName.append(";");
				handleName.append(task.getName());
				
				// 任务执行人
				if (StringUtil.isNotEmpty(task.getAssigneeId()) && !"0".equals(task.getAssigneeId())) {
					com.hotent.org.api.model.User user = getMoiUserService().getById(task.getAssigneeId());
					if(user != null){
						handler.append(user.getFullname());
					}
				}
				// 任务执行集合
				List<BpmIdentity> identities = getBpmTaskService().getTaskCandidates(task.getId());
				for (BpmIdentity bpmIdentity : identities) {
					if (StringUtil.isNotEmpty(handler.toString()))
						handler.append(";");
					handler.append(bpmIdentity.getName());
				}
			}
			pageContext.getOut().print("<td>"+handleName+"</td><td>"+handler+"</td>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	// -------------------get/set----------------------
	
	
	public MoiUserService getMoiUserService() {
		if (this.moiUserService == null) {
			this.moiUserService = (MoiUserService) getRequestContext()
					.getWebApplicationContext().getBean("moiUserService");
		}
		return moiUserService;
	}

	/**
	 * @return the bpmTaskService
	 */
	public BpmTaskService getBpmTaskService() {
		if (this.bpmTaskService == null) {
			this.bpmTaskService = (BpmTaskService) getRequestContext()
					.getWebApplicationContext().getBean("bpmTaskService");
		}
		return bpmTaskService;
	}

	/**
	 * @return the proInstID
	 */
	public String getProInstID() {
		return proInstID;
	}

	/**
	 * @param proInstID the proInstID to set
	 */
	public void setProInstID(String proInstID) {
		this.proInstID = proInstID;
	}

}
